<?php
namespace api\token;

use common\models\Wxuser;

class tokenCheck {
    public function __construct()
    {}

    /**
     * @param $userId
     * @param $access_token
     * @return bool
     */
    public function check($userId,$access_token)
    {
        $user = Wxuser::findOne([$userId]);
       if($user->deadline > time()){
           return false;
       }
       if($access_token == Wxuser::findOne([$userId])->access_token){
           return true;
       }
        return false;
    }

    /**
     * @param $userId
     * @param $openId
     * @return array|null|\yii\db\ActiveRecord
     */
    public function reftoken($userId,$openId)
    {
        $user = Wxuser::find()->where(['id'=>$userId,'openid'=>$openId])->one();
        if($user){
            $user->deadline = time()+3600*2;
            $user->access_token = md5(rand(1000,10000));
            if($user->save()){
                return $user;
            }else{
                return null;
            }
        }
        return null;
    }
}